﻿using Abp.Web.Models;
using Abp.Web.Mvc.Authorization;
using MyCompanyName.AbpZeroTemplate.Authorization;
using MyCompanyName.AbpZeroTemplate.Authorization.Users;
using MyCompanyName.AbpZeroTemplate.Product;
using MyCompanyName.AbpZeroTemplate.Product.Dto;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace MyCompanyName.AbpZeroTemplate.Web.Areas.Mpa.Controllers
{

    public class ProductController : Controller
    {
        private readonly IUserAppService _userAppService;
        private readonly IProductService _productAppService;
        public ProductController(IUserAppService userAppService, IProductService productAppService)
        {
            this._userAppService = userAppService;
            this._productAppService = productAppService;
        }
        // GET: Mpa/Product   
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取产品信息
        /// </summary>
        /// <param name="productid"></param>
        /// <returns></returns>
        public ActionResult CreteOrUpdate(string ProductId)
        {
            ProductInputDto product = new ProductInputDto();
            if (!ProductId.Equals("0"))
            {
                product = _productAppService.GetProductById(Int32.Parse(ProductId));
            }
            return View(product);
        }

        /// <summary>
        /// 创建产品
        /// </summary>
        /// <param name="productid"></param>
        /// <returns></returns>
        public JsonResult CreateProductRequest(CreateProductInputDto input)
        {
            bool IsSuccess = true;
            string ErrorMsg = string.Empty;

            try
            {
                _productAppService.CreateProduct(input);
            }
            catch (Exception ex)
            {
                IsSuccess = false;
                ErrorMsg = "创建失败" + ex.Message;
            }
            return Json(
                new
                {
                    status = IsSuccess,
                    errormsg = ErrorMsg
                });
        }

        /// <summary>
        /// 更新产品
        /// </summary>
        /// <param name="productid"></param>
        /// <returns></returns>
        public JsonResult UpdateProductRequest(UpdateProductDto input)
        {
            bool IsSuccess = true;
            string ErrorMsg = string.Empty;

            try
            {
                _productAppService.UpdatePorduct(input);
            }
            catch (Exception ex)
            {
                IsSuccess = false;
                ErrorMsg = "更新失败" + ex.Message;
            }

            return Json(new
            {
                status = IsSuccess,
                errormsg = ErrorMsg
            });
        }



        public ActionResult ProductList()
        {
            return View();
        }

        /// <summary>
        /// 异步获得的DataTable数据
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        /// 
        [DontWrapResult]
        public ActionResult GetObjectDataTable(int offset, int limit)
        {
            //参数
             string Filter1SearchNamea = Request["Filter1SearchName"];
            //获得数据
            var result = _productAppService.GetProductList(Filter1SearchNamea,"");
            var resultGet = result.Skip(offset).Take(limit).ToList();
            object data = new { total = result.Count, rows = resultGet };

            return Json(data, JsonRequestBehavior.AllowGet);


        }
    }
}